package com.zut.subject.domain.service.impl;
import com.zut.subject.common.enums.IsDeletedFlagEnum;
import com.zut.subject.domain.convert.ConvertSubjectInfo;
import com.zut.subject.domain.entity.SubjectInfoBo;
import com.zut.subject.domain.entity.SubjectOptionAnswerBo;
import com.zut.subject.domain.handler.subject.SubjectHandlerFactory;
import com.zut.subject.domain.handler.subject.SubjectTypeHandler;
import com.zut.subject.domain.service.SubjectInfoDomainService;
import com.zut.subject.infra.basic.entity.SubjectInfo;
import com.zut.subject.infra.basic.entity.SubjectMapping;
import com.zut.subject.infra.basic.service.SubjectInfoService;
import com.zut.subject.infra.basic.service.SubjectLabelService;
import com.zut.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@Service
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {
    @Resource
    private SubjectInfoService subjectInfoService;

    @Resource
    private SubjectHandlerFactory subjectHandlerFactory;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectLabelService subjectLabelService;

    //添加题目，
    @Override
    public boolean add(SubjectInfoBo subjectInfoBo) {


        if (log.isInfoEnabled()){
            log.info("domain接受为：{}",subjectInfoBo);
        }


        SubjectInfo subjectInfo = ConvertSubjectInfo.INSTANCE.toSubjectInfo(subjectInfoBo);
        if (log.isInfoEnabled()){
            log.info("向infra传入的题目信息为：{}",subjectInfo);
        }
        //修改题目状态为未被删除
        subjectInfo.setIsDelete(0);
        //传入infra层插入题目信息表
        int insert=subjectInfoService.insert(subjectInfo);
        //取得题目类型，为下面不同题目类型有不同处理方法做准备
        Integer subjectType = subjectInfoBo.getSubjectType();
        //工厂＋策略模式，避免出现大类if else 使代码堆积，不易修改
        //创建题目类型处理工厂，将要处理的题目类型传入，传出处理方案
        SubjectTypeHandler handler = subjectHandlerFactory.getHandlerBySubjectTypeId(subjectType);
        //插入题目信息后返回本题目id，给domain层传入的bo赋值
        subjectInfoBo.setId(subjectInfo.getId());
        //将bo对象传入处理方法中，让不同的题目有不同的处理
        handler.add(subjectInfoBo);

        //（多对多，中间表）给题目分类标签分类映射表插入数据
        List<SubjectMapping> mappings =new LinkedList<>();
        List<Integer> categoryIds = subjectInfoBo.getCategoryIds();
        List<Integer> labelIds = subjectInfoBo.getLabelIds();
        categoryIds.forEach(categoryId-> {
            labelIds.forEach(labelId->{
                SubjectMapping mapping =new SubjectMapping();
                mapping.setCategoryId(categoryId);
                mapping.setLabelId(labelId);
                mapping.setSubjectId(subjectInfoBo.getId());
                mapping.setIsDelete(IsDeletedFlagEnum.UN_DELETED.getCode());
                mappings.add(mapping);
            });
        });
        //传入infra层去给mapping表插入数据
        subjectMappingService.batchInsert(mappings);
        return insert>0;
    }


    //查询题目列表
    @Override
    public List<SubjectInfoBo> getSubjectPage(SubjectInfoBo subjectInfoBo) {
        SubjectInfo subjectInfo = ConvertSubjectInfo.INSTANCE.toSubjectInfo(subjectInfoBo);
        int count = subjectInfoService.queryCount(subjectInfo, subjectInfoBo.getLabelId(), subjectInfoBo.getCategoryId());
        int start = (subjectInfoBo.getPageNo()-1)*subjectInfoBo.getPageSize();
        int pageSize =subjectInfoBo.getPageSize();

        if (count==0){
            return null;
        }else{
            List<SubjectInfo> subjectInfoList=subjectInfoService.queryPage(subjectInfo, subjectInfoBo.getLabelId(), subjectInfoBo.getCategoryId(),start,pageSize);
            List<SubjectInfoBo> subjectInfoBoList = ConvertSubjectInfo.INSTANCE.toListSubjectInfoBo(subjectInfoList);
            return subjectInfoBoList;
        }


    }


    /**
     * 查询题目详情
     * @param subjectInfoBo
     * @return
     */
    @Override
    public SubjectInfoBo querySubjectInfo(SubjectInfoBo subjectInfoBo) {
        Integer SubjectId = subjectInfoBo.getId();
        SubjectInfo subjectInfo=subjectInfoService.queryById(SubjectId);

        SubjectTypeHandler handler = subjectHandlerFactory.getHandlerBySubjectTypeId(subjectInfo.getSubjectType());
        SubjectInfoBo Bo1 = handler.querySubjectOption(SubjectId);
        SubjectInfoBo Bo = ConvertSubjectInfo.INSTANCE.toSubjectBo(subjectInfo);
        Bo.setSubjectAnswer(Bo1.getSubjectAnswer());
        Bo.setOptionList(Bo1.getOptionList());
        //Bo的labelName集合要赋值
        List<Integer> label =subjectMappingService.queryLabelIdBySubjectId(SubjectId);
        List<String> labelName=subjectLabelService.queryNameById(label);
        Bo.setLabelName(labelName);

        return Bo;
    }
}
